Is there a difference between `==` and `is` in python?
Posted
by Bernard
on Stack Overflow
See other posts from Stack Overflow
or by Bernard
Published on 2008-09-25T12:27:09Z
Indexed on
2010/05/28
21:32 UTC
Read the original article
Hit count: 198
My Google-fu has failed me.
In Python, are these:
n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
two tests for equality equivalent (ha!)? Does this hold true for objects where you would be comparing instances (a list
say)?
Okay, so this kind of answers my question:
l = list()
l.append(1)
if l == [1]:
print 'Yay!'
# Holds true, but...
if l is [1]:
print 'Yay!'
# Doesn't.
So ==
tests value where is
tests to see if they are the same object?
© Stack Overflow or respective owner